<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const account = '123';
        const password = '123';
        let chance = 3;  // 登录的总次数
        let money = 1000; // 账户余额

        let isLoop = true;


        login();

        // 登录功能
        function login() {
            while (chance > 0) {
                const input_account = prompt('请输入账号：');
                const input_password = prompt('请输入密码：');

                if (input_account == account && input_password == password) {
                    console.log("登录成功！");
                    atm();
                } else {
                    chance--;
                    if (chance > 0) {
                        console.log(`账号或密码错误，你还有${chance}次机会`);
                    } else {
                        console.log('登录次数已用完，再见！');
                    }
                }
            }
        }

        // 存款操作
        function save() {
            while (true) {
                const saveMoney = prompt('请输入存款金额：') - 0;
                // money = money + saveMoney;
                money += saveMoney;
                console.log(`存款金额为:${saveMoney}, 账户余额为:${money}`);
                const isContinue = prompt('是否继续存款：(y/n)');
                if (isContinue === 'n') {
                    break; // 结束最近的while
                }
            }
        }
        
        // 取款操作
        function get() {
            while (true) {
                const getMoney = prompt('请输入取款金额：') - 0;
                // money = money + getMoney;
                money += getMoney;
                console.log(`取款金额为:${getMoney}, 账户余额为:${money}`);
                const isContinue = prompt('是否继续取款：(y/n)');
                if (isContinue === 'n') {
                    break; // 结束最近的while
                }
            }
        }

        // 退出atm
        function logout() {
            console.log("谢谢使用再见！");
            // 结束内部小循环
            isLoop = false;
            // 结束外部大循环
            chance = 0;
        }

        // atm操作流程
        function atm() {
            while (isLoop) {
                const option = prompt("请选择业务: 1.查询 2.存款 3.取款 4.退出");
                switch (option) {
                    case "1":
                        console.log(`账户余额为: ${money}`);
                        break;
                    case "2":
                        save();
                        break; // 结束switch
                    case "3":
                        get();
                        break;
                    case "4":
                        logout();
                        // 结束switch
                        break;
                }
            }
        }


    </script>
</body>

</html>